﻿//
// Copyright(C) MixModes Inc. 2011
// 

using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;

namespace MixModes.Synergy.Web.VisualFramework.Triggers
{
    /// <summary>
    /// Initiates a storyboard in response to an event
    /// </summary>
    public class EventToVisualStateTrigger : TriggerAction<FrameworkElement>
    {
        /// <summary>
        /// Command property
        /// </summary>
        public static DependencyProperty VisualStateProperty = DependencyProperty.Register("VisualState",
                                                                                          typeof(VisualState),
                                                                                          typeof(EventToVisualStateTrigger),
                                                                                          new PropertyMetadata(null));

        /// <summary>
        /// Gets or sets the state of the visual.
        /// </summary>
        /// <value>The state of the visual.</value>
        public VisualState VisualState
        {
            get
            {
                return GetValue(VisualStateProperty) as VisualState;
            }
            set
            {
                SetValue(VisualStateProperty, value);
            }
        }

        /// <summary>
        /// Invokes the action.
        /// </summary>
        /// <param name="parameter">The parameter to the action. If the action does not require a parameter, the parameter may be set to a null reference.</param>
        protected override void Invoke(object parameter)
        {
            Storyboard storyBoard;
            if ((VisualState != null) && ((storyBoard = VisualState.Storyboard) != null))
            {
                storyBoard.Begin();                
            }
        }
    }
}
